﻿using MediatR;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;
using W.Purchase.Read.API.AppLication.Command;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler
{
    public class VendorShowFanCommandHandler : IRequestHandler<VendorShowFanCommand, ApiResult<Vendor>>
    {
        private readonly IVendorRepository _vendorRepository;
        private readonly ILogger<VendorShowFanCommandHandler> _logger;
        public VendorShowFanCommandHandler(IVendorRepository vendorRepository, ILogger<VendorShowFanCommandHandler> logger)
        {
            _vendorRepository = vendorRepository;
            _logger = logger;
        }
        public async Task<ApiResult<Vendor>> Handle(VendorShowFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Vendor> result = new ApiResult<Vendor>();
            try
            {
                var res = (await _vendorRepository.GetAllAsaync()).Where(x => request.vendorId.Contains(x.VendorId)).ToList();

                result.Data = res.FirstOrDefault();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                throw;
            }
            return result;
        }
    }
}
